package com.edroid.apkhooker.home;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Toast;

import com.example.service.SystemServiceGetter;

public class BaseListFragment extends ListFragment implements OnItemLongClickListener {
	protected int pressedIndex;
	
	protected boolean hook(AppEntry appEntry) {
		try {
			SystemServiceGetter.getSystemService().watch(appEntry.getPackgeName());
			Toast.makeText(getActivity(), "hook suc!", Toast.LENGTH_SHORT).show();
			return true;
		} catch (Exception e) {
			Toast.makeText(getActivity(), "设置监控失败！", Toast.LENGTH_SHORT).show();
			e.printStackTrace();
		}
		
		return true;
	}

	protected boolean unhook(AppEntry appEntry) {
	    try {
	        SystemServiceGetter.getSystemService().unWatch(appEntry.getPackgeName());
	        Toast.makeText(getActivity(), "unhook suc!", Toast.LENGTH_SHORT).show();
	        return true;
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    
	    return true;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		getListView().setOnItemLongClickListener(this);
		registerForContextMenu(getListView());
	}
	
	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
		pressedIndex = position;
		return false;
	}
}
